<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>UrlDownloadToFile</title>
<meta name="description" content="Download any type of file via UrlDownloadToFile in this free scripting language. e.g. UrlDownloadToFile, http://www.anyorg.org/File.htm, c:\File.htm">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>UrlDownloadToFile</h1>

<p>Downloads a file from the Internet.</p>

<pre class="Syntax">UrlDownloadToFile, URL, Filename</pre>
<h3>Parameters</h3>
<dl>

  <dt>URL</dt>
  <dd><p>URL of the file to download. For example, http://someorg.org might retrieve the welcome page for that organization.</p></dd>

  <dt>Filename</dt>
  <dd><p><strong>Download to a file</strong>: Specify the name of the file to be created locally, which is assumed to be in <a href="../Variables.htm#WorkingDir">%A_WorkingDir%</a> if an absolute path isn't specified. Any existing file will be <strong>overwritten</strong> by the new file.</p>
    <p><strong>Download to a variable</strong>: See the <a href="#WHR">example</a> below.</p></dd>

</dl>

<h3>ErrorLevel</h3>
<p><span class="ver">[v1.1.04+]</span> This command is able to throw an exception on failure. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to 1 if there was a problem or 0 otherwise.</p>
<h3>Remarks</h3>
<p>The download might appear to succeed even when the remote file doesn't exist. This is because many web servers send an error page instead of the missing file. This error page is what will be saved in place of <em>Filename</em>.</p>
<p>Internet Explorer 3 or greater must be installed for this function to work. Firewalls or the presence of multiple network adapters may cause this function to fail. Also, some websites may block such downloads.</p>
<p><strong>Caching</strong>:</p>
<ul>
  <li>In v1.0.44.07+, the URL is retrieved directly from the remote server (that is, never from Internet Explorer's cache). To permit caching, precede the URL with *0 followed by a space; for example: <code>*0 http://someorg.org</code>. The zero following the asterisk may be replaced by any valid dwFlags number; for details, search <a href="http://www.microsoft.com">www.microsoft.com</a> for InternetOpenUrl.</li>
  <li>In versions older than 1.0.44.07, the file is retrieved from the cache whenever possible. To avoid this, specify a query string at the end of the URL. For example: <code>http://www.someorg.org/doc.html?fakeParam=42</code>. Note: If you download the same file frequently, the query string should be varied.</li>
</ul>
<p><strong>Proxies</strong>: UrlDownloadToFile will use a proxy server to access the Internet if such a proxy has been configured in Microsoft Internet Explorer's settings.</p>
<p><strong>FTP and Gopher</strong>: v1.0.48.04+ supports FTP and Gopher URLs. For example:</p>
<pre>UrlDownloadToFile, ftp://example.com/home/My File.zip, C:\My Folder\My File.zip  <em>; Log in anonymously.</em>
UrlDownloadToFile, ftp://user:pass@example.com:21/home/My File.zip, C:\My Folder\My File.zip  <em>; Log in as a specific user.</em>
UrlDownloadToFile, ftp://user:pass@example.com/My Directory, C:\Dir Listing.html  <em>; Gets a directory listing in HTML format.</em></pre>
<h3>Related</h3>
<p><a href="FileRead.htm">FileRead</a>, <a href="FileCopy.htm">FileCopy</a></p>
<h3>Examples</h3>
<pre class="NoIndent">UrlDownloadToFile, http://ahkscript.org/download/1.1/version.txt, C:\AutoHotkey Latest Version.txt
UrlDownloadToFile, http://someorg.org/archive.zip, C:\SomeOrg's Archive.zip</pre>
<pre id="WHR" class="NoIndent"><em>; Example: Download text to a variable:</em>
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "http://ahkscript.org/download/1.1/version.txt", true)
whr.Send()
<em>; Using 'true' above and the call below allows the script to remain responsive.</em>
whr.WaitForResponse()
version := whr.ResponseText
MsgBox % version
</pre>
<pre id="XHR" class="NoIndent"><em>; Example: Make an asynchronous HTTP request.</em>

req := ComObjCreate("Msxml2.XMLHTTP")
<em>; Open a request with async enabled.</em>
req.open("GET", "http://ahkscript.org/download/1.1/version.txt", true)
<em>; Set our callback function (v1.1.17+).</em>
req.onreadystatechange := Func("Ready")
<em>; Send the request.  Ready() will be called when it's complete.</em>
req.send()
<em>/*
; If you're going to wait, there's no need for onreadystatechange.
; Setting async=true and waiting like this allows the script to remain
; responsive while the download is taking place, whereas async=false
; will make the script unresponsive.
while req.readyState != 4
    sleep 100
*/</em>
#Persistent

Ready() {
    global req
    if (req.readyState != 4)  ; Not done yet.
        return
    if (req.status == 200 || req.status == 304) ; OK.
        MsgBox % "Latest AutoHotkey version: " req.responseText
    else
        MsgBox 16,, % "Status " req.status
    ExitApp
}</pre>

</body>
</html>
